package com.haierCamera.customapplication.ui.login;

import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import com.haierCamera.customapplication.BaseActivity;
import com.haierCamera.customapplication.R;
import com.haierCamera.customapplication.api.repo.RegisterRepo;
import com.haierCamera.customapplication.databinding.HzklActivityRegisterBinding;
import com.haierCamera.customapplication.databinding.HzklActivityWxPhoneBinding;
import com.haierCamera.customapplication.ui.WebViewPublicActivity;
import com.haierCamera.customapplication.utils.EditTextChangeBoolean;
import com.haierCamera.customapplication.utils.android.SystemUIHelper;

import javax.inject.Inject;

public class HZKLWXphoneActivity extends BaseActivity {
    private HzklActivityWxPhoneBinding binding;
    @Inject
    RegisterRepo repo;
    ////密码只包含字母，数字，字符中至少两种
    private static final String regex = "^(?![0-9]+$)(?![a-zA-Z]+$)[a-zA-Z0-9]{8,32}$";
    private String openId;
    boolean phoneEnable, pwdEnable, aBoolean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.hzkl_activity_wx_phone);
        initData();
    }

    private void initData() {
        binding.registerBack.setOnClickListener(v -> finish());
        binding.userXieYi.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //下划线
        binding.userXieYi.getPaint().setAntiAlias(true);//抗锯齿
        openId = getIntent().getStringExtra("openId");
        binding.userXieYi.setOnClickListener(view -> startActivity(new Intent(HZKLWXphoneActivity.this, WebViewPublicActivity.class)
                .putExtra("title", "").putExtra("url", "http://edu-wc.nullehome.com/servicePotocol/#/")));
        binding.registerBtn.setOnClickListener(view -> registerUser());
        binding.registerCheck.setOnCheckedChangeListener((compoundButton, b) -> aBoolean = b);
        new EditTextChangeBoolean().buttonEnable(binding.registerPhone, enable -> binding.registerBtn.setEnabled(enable));
    }

    private void registerUser() {
        String phone = binding.registerPhone.getText().toString();
        if (TextUtils.isEmpty(phone)) {
            Toast.makeText(this, "请输入账号", Toast.LENGTH_SHORT).show();
            return;
        }
        if (!aBoolean) {
            Toast.makeText(this, "请阅读并勾选《四川广电条款》", Toast.LENGTH_SHORT).show();
            return;
        }
        repo.getVcode(phone).observe(this, objectResource -> {
            switch (objectResource.status) {
                case LOADING:
                    break;
                case ERROR:
                    Toast.makeText(HZKLWXphoneActivity.this, objectResource.errorMessage, Toast.LENGTH_SHORT).show();
                    break;
                case SUCCESS:
                    startActivityForResult(new Intent(HZKLWXphoneActivity.this, HZKLWXVcodeActivity.class)
                            .putExtra("phone", phone).putExtra("openId", openId), 100);
                    break;
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100 && resultCode == RESULT_OK) {
            setResult(RESULT_OK);
            finish();
        }
    }
}
